// pages/approve/approve.js
var app = getApp();
var com = require('../../common.js');
Page({
  data: {
    info: {},
    date: '2000-01-01',
    click: false
  },
  onLoad: function(options) {
    var _this = this
    _this.onBankInfo()
  },
  onReady: function() {},
  onShow: function() {},
  onHide: function() {},
  onUnload: function() {},
  onPullDownRefresh: function() {},
  onReachBottom: function() {},
  // onShareAppMessage: function() {},
  onBankInfo() {
    var _this = this
    com.link(app.link.sub, {}, function(reponse) {
      console.log(reponse)
      _this.setData({
        info: reponse.data.response
      })
    }, "GET")
  },
  //信息存入
  onBindInput(e) {
    console.log(e)
    var _this = this,
      i = e.currentTarget.id,
      t = e.detail.value,
      l = _this.data.info
    if (i == 1) {
      l.info.legal_person = t
    } else if (i == 2) {
      l.info.phone_number = t
    } else if (i == 3) {
      l.info.legal_idcode = t
    }
    _this.setData({
      info: l
    })
  },
  onPickerTap(e) {
    var _this = this,
      id = e.currentTarget.id,
      list = _this.data.info
    if (id == 1) {
      list.sub.valid_from = e.detail.value
    } else if (id == 2) {
      list.sub.valid_until = e.detail.value
    } else if (id == 3) {
      list.sub.license_valid_from = e.detail.value
    } else if (id == 4) {
      list.sub.license_valid_until = e.detail.value
    }
    _this.setData({
      info: list
    })
  },
  //上传照片方法

  onShowFile(e) {
    var _this = this,
      id = e.currentTarget.id,
      list = _this.data.info
    wx.chooseImage({
      count: 1,
      success: function(res) {
        console.log(res)
        if (id == 1) {
          list.info.license_url = res.tempFilePaths[0]
        } else if (id == 2) {
          list.info.positive_url = res.tempFilePaths[0]
        } else if (id == 3) {
          list.info.other_url = res.tempFilePaths[0]
        }
        _this.setData({
          info: list
        })
        wx.uploadFile({
          url: app.link.temporary, //为后端上传代码方法
          filePath: res.tempFilePaths[0],
          name: 'file',
          formData: {
            file: res.tempFilePaths[0]
          },
          success: function(res) {
            let img = JSON.parse(res.data)
            console.log(img.response.file.name)
            if (id == 1) {
              list.info.license = img.response.file.name
            } else if (id == 2) {
              list.info.positive = img.response.file.name
            } else if (id == 3) {
              list.info.other = img.response.file.name
            }
            _this.setData({
              info: list
            })
            console.log(_this.data)
          }
        })
      }
    })
  },
  onUploadInfo(e) {
    // wx.navigateTo({
    //   url: '/pages/bankUser/bankUser',
    // })
    var _this = this,
      info = _this.data.info.info,
      check = _this.data.click
    if (check) {
      info.valid_until = '9999-12-31'
    } else {
      info.valid_until = _this.data.info.sub.valid_until
    }
    info.license_valid_from = _this.data.info.sub.license_valid_from
    info.license_valid_until = _this.data.info.sub.license_valid_until
    info.valid_from = _this.data.info.sub.valid_from
    console.log(info);
    com.link(app.link.sub, info, function(reponse) {
      console.log(reponse)
      if (reponse.data.state) {
        wx.showModal({
          title: '温馨提示',
          content: reponse.data.message,
          showCancel: false,
          success(res) {
            if (res.confirm) {
              wx.navigateBack({
                delta: 1
              })
            }
          }
        })
      } else {
        wx.showModal({
          title: '温馨提示',
          content: reponse.data.message,
          showCancel: false
        })
      }
    })
  },
  cardTap() {
    var _this = this,
      l = _this.data.click
    l = l ? false : true
    _this.setData({
      click: l,
    })

    console.log(_this.data.info.sub)
  }
})